/**
 * This is a simple implementation of search bar (or highlighter) codes
 * @author Jeky, WANG jiechao
 */
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JTextPane;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.BadLocationException;
import javax.swing.JOptionPane;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



public class SearchBar extends JTextField implements ActionListener{

	private static final long serialVersionUID = 1L;
	JButton searchButton;//This is the button to trigger the highlight function
	JTextField textField;//This is the area in the small search bar 
	boolean firstClick=true;
	JTextPane text;
	PopupListener PL;
	boolean isFirst=true;
	
	
	//public SearchBar(JTextPane text){//words in the main text area is input to this method
    public SearchBar(){

	searchButton = new JButton(new ImageIcon("search.png"));
	searchButton.setToolTipText("Click to highlight");//when the mouse stay on it, the word appears

	textField = new JTextField("used for highlighting",20);//create the search bar with the default word"used for searching" 
	//this.text=text;
	}
	
	public JButton getSearchButton(){//This is for program in the Group.java to use this button
		return searchButton;
	}
	
	public JTextField getSearchBar(){//This is for program in the Group.java to use this search bar
		return textField;
	}
	
	public void myAction(JTextPane text){	
		PL=new PopupListener();
        this.text = text;
		String textInText = text.getText().replaceAll(String.valueOf((char) 13), "");     //this also solves a bug when an "enter" is typed in
        text.setText(textInText);                                                         //remove "enter"                                                   
        String keyword = textField.getText();                                             //get the text in the keyword bar
        int index = textInText.indexOf(keyword);                                          //return the first location of keyword
        text.addMouseListener(PL);//add listener
        
        if (!isFirst){
        	if (firstClick )
        		JOptionPane.showMessageDialog(SearchBar.this,"Please type in words");        
        	else if (index != -1  ) {                                                                //when there is any matched
        		text.getHighlighter().removeAllHighlights();                                  //in a loop, first remove highlight, then add highlight
        		try {
        			while (index != -1 & !keyword.equals("") ) {                              //if any string in the main text area matches the keyword
        				text.getHighlighter().addHighlight(index, index + keyword.length(), DefaultHighlighter.DefaultPainter);//highlight the keyword and search from the location behind the current keyword
        				//text.setSelectionStart(index);//from index to perform replace all
        				//System.out.print("q121");
        	            //text.setSelectionEnd(index);//perform replace all until index+find.length()
        				index = textInText.indexOf(keyword, index + keyword.length());                       
        			}
        		} 
        		catch (BadLocationException ex) {
        			ex.printStackTrace();
        		}
        	}
        	else 
        		JOptionPane.showMessageDialog(SearchBar.this,"Can't find keyword:" + keyword);//when there is no matched,a dialog emerges     
        }
	}
	public void actionPerformed(ActionEvent e) {
		
	}
    class PopupListener extends MouseAdapter {
    	public void mousePressed(MouseEvent e) {
    		if(e.getButton() == MouseEvent.BUTTON1)
    			text.getHighlighter().removeAllHighlights();     			
        }
    }
}
